<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Com extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'com';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'contract_status_text',
        'contract_start_time_text',
        'contract_end_time_text',
        'pay_status_text'
    ];
    

    
    public function getContractStatusList()
    {
        return ['0' => __('Contract_status 0'), '1' => __('Contract_status 1')];
    }

    public function getPayStatusList()
    {
        return ['0' => __('Pay_status 0'), '1' => __('Pay_status 1')];
    }


    public function getContractStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['contract_status']) ? $data['contract_status'] : '');
        $list = $this->getContractStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getContractStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['contract_start_time']) ? $data['contract_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getContractEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['contract_end_time']) ? $data['contract_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setContractStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setContractEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function stand()
    {
        return $this->belongsTo('Stand', 'stand_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
